package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.CouponInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.CoupinsDetailsP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.CoupinsDetailsView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
*
* 优惠券明细
* */
public class CoupinsDetailsActivity extends BaseActivity implements CoupinsDetailsView {
Intent intent;
Bundle bundle;
CouponInfo couponInfo;
@BindView(R.id.into_cg)
ImageView intoCg;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.my_recy)
RecyclerView myRecy;
@BindView(R.id.my_srf)
SwipeRefreshLayout mySrf;
List<CouponInfo> couponInfos;
BaseQuickAdapter<CouponInfo> baseQuickAdapter;
Date d1;
CoupinsDetailsP coupinsDetailsP;
int curpage = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coupins_details);
    AppManager.getAppManager().addActivity(this);
    ButterKnife.bind(this);
    initView();
}

private void initView() {
    intent = getIntent();
    bundle = intent.getExtras();
    couponInfo = (CouponInfo) bundle.getSerializable(Constants.COUPONS);
    setToolBar(toolbar, couponInfo.getCard_cgname(), true);
    couponInfos = new ArrayList<>();
    coupinsDetailsP = new CoupinsDetailsP(this);
    setRefresh(mySrf);
    setRecy(myRecy);
    initAdapter();
    initEvent();
}

private void initEvent() {
    baseQuickAdapter.setOnLoadMoreListener(() -> {
        curpage++;
        addData();
    });
    mySrf.setOnRefreshListener(() -> {
        curpage = 1;
        addData();
    });
    toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(CoupinsDetailsActivity.class));
}

private void initAdapter() {
    baseQuickAdapter = new BaseQuickAdapter<CouponInfo>(R.layout.item_coupomdetail, couponInfos) {
        @Override
        protected void convert(BaseViewHolder helper, CouponInfo item) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(d1);
            cal.add(Calendar.DATE, item.getCard_days());

            Calendar fa = Calendar.getInstance();
            fa.setTime(new Date(item.getRegtime()));
            fa.add(Calendar.DATE, 1);

            helper.setText(R.id.tv_coupinsmsg, "消费满" + item.getCard_upperlimit() + "可以使用")
                    .setText(R.id.tv_time, new SimpleDateFormat("yyyy.MM.dd").format(fa.getTime()) +
                            "~" + (new SimpleDateFormat("yyyy.MM.dd")).format(cal.getTime()))
                    .setText(R.id.tv_pastdue, "￥" + item.getCard_value());
        }
    };
    baseQuickAdapter.openLoadAnimation();
    baseQuickAdapter.openLoadMore(10);
    myRecy.setAdapter(baseQuickAdapter);
    addData();
}

private void addData() {
    coupinsDetailsP.addData(Model.HTTPURL + Model.NEWCOUPONSDETAILS + "username=" + getString(Constants.USERPHONE) + "&cgid=" +
            couponInfo.getCard_cgid()
            + "&curpage=" + curpage + "&pageno=10");
}

@Override
public void sendRes(List<CouponInfo> couponInfoList, Date gtm) {
    this.d1 = gtm;
    if (curpage == 1) {
        baseQuickAdapter.setNewData(couponInfoList);
    } else {
        baseQuickAdapter.addData(couponInfoList);
    }

    if (couponInfoList.size() != 10) {
        baseQuickAdapter.loadComplete();
        addFootView(baseQuickAdapter);
    }

}

@Override
public void showdialog() {
    showDialog();
}

@Override
public void dissmissdialog() {
    dissMissDialog();
    mySrf.setRefreshing(false);
}

@OnClick(R.id.into_cg)
public void onClick() {
    bundle.putString(Constants.TAG, CoupinsDetailsActivity.class.getName());
    bundle.putString(Constants.CG_ID, String.valueOf(couponInfo.getCard_cgid()));
    startActivity(RestaurantPageActivity.class, bundle);
}
}
